import axios from 'axios'
import qs from 'qs'// 这个只要安装了axios就可以直接引入
let http = axios.create({
  baseURL: '/',
  timeout: 5000,
  headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})

http.param = (data = {}, contentType = 'application/x-www-form-urlencoded') => {
  return contentType === 'application/x-www-form-urlencoded' ? qs.stringify(data) : (contentType === 'application/json' ? JSON.stringify(data) : data)
}
/**
 * get封装
 * @params:url
 * @params:params
 */
export function httpGet (url, params = {}) {
  return new Promise((resolve, reject) => {
    http.get(
       url, {params: params})
      .then(response => {
        resolve(response.data)
      })
      .catch(err => {
        reject(err)
      })
  })
}

/**
 * post封装
 * @params:url
 * @params:param
 */
export function httpPost (url, param = {}) {
  return new Promise((resolve, reject) => {
    http.post(url, http.param(param))
      .then(response => {
        resolve(response.data)
      })
      .catch(err => {
        reject(err)
      })
  })
}
